import { MapData, type FeatureDataInterface, type FeatureInterface, type MapDataInterface } from "@/class/map/MapData";
import { convert_gcj02_To_Gps84, convert_gps84_To_Gcj02, MapCoordSys } from "@/class/map/myMapOpt";
import { reqConvertGps84ToGcj02, reqDeleteMapData } from "@/api/mapdata";
import { reqPostPolyline, reqPutPolyline } from "@/api/polyline"
import { requestGetData, requestIsSuccess } from "@/api/request";
import { getRandomColor, getUUID, toJson } from "@/utils/utils";
import type { Feature } from "./Feature";


export class PolylineData implements FeatureDataInterface {
    featureData: MapDataInterface;
    featureMap: Polyline;
    constructor({ featureData, featureMap }: {
        featureData: MapDataInterface;
        featureMap: Polyline;
    }) {
        this.featureData = new MapData(featureData)
        this.featureMap = new Polyline(featureMap)
    }
    /**
     * 移动到该要素
     * @param map
     */
    moveTo(map: AMap.Map) {
        const element = this.getDateInMap(map)
        var bounds = element.getBounds();
        map.setBounds(bounds)
        this.blinkData(map)
        /*const path = this.polyline.path
        const getBound = getBounds();
        const result =getBound(path);
        const bounds = new AMap.Bounds(result.southWest,result.northEast);
        map.setBounds(bounds,false,[0,0,0,0])*/
    }
    /**
     * 添加要素，并返回地图要素
     * @param map
     * @returns {*}
     */
    addToMap(map: AMap.Map, mapCoordSys:MapCoordSys) {
        if (this.featureData.id === undefined) {
            this.featureData.id = getUUID();
        }
        if (this.featureMap.id === undefined) {
            this.featureMap.id = getUUID();
        } const result = this.getBaiduMapOpt(mapCoordSys)
        const feature = new AMap.Polyline(result);
        feature.setExtData({
            id: this.featureData.id,
            name:this.featureData.name,
        })
        map.add(feature)
        return feature
    }

    /**
     * 返回地图需要的数据，如果坐标不是CJ02，转换为CJ02
     * @returns {Promise<any>}
     */
    getBaiduMapOpt(mapCoordSys:MapCoordSys) {
        const opt = JSON.parse(JSON.stringify(this.featureMap))
        //坐标转换
        switch (mapCoordSys) {
            case MapCoordSys.CJ02:
                if (this.featureData.coordsys !== MapCoordSys.CJ02)
                    opt.path = convert_gps84_To_Gcj02(opt.path)
                break;
            case MapCoordSys.GPS:
                if (this.featureData.coordsys !== MapCoordSys.GPS)
                    opt.path = convert_gcj02_To_Gps84(opt.path)
                break;
        }
        return opt
    }


    /**
     * 更新面数据
     * @returns true:更新成功;false:更新失败
     */
    async update() {
        const result = await reqPutPolyline(this)
        return true
    }
    /**
     * 添加面数据
     * @returns true:更新成功;false:更新失败
     */
    async upload() {
        const result = await reqPostPolyline(this)
        return true
    }
    /**
     * 返回要素的原始设置
     * @returns {Marker}
     */
    getElement() {
        return this.featureMap
    }
    /**
     * 判断能否正确添加到该地图
     * @returns {boolean}
     */
    isElement() {
        return (this.featureMap.path as []).length > 0
    }
    /**
     * 返回复制对象
     * @returns {MarkerData}
     */
    getCopy() {
        return new PolylineData(JSON.parse(JSON.stringify(this)))
    }

    /**
     * 返回地图中的对象
     * @param map
     * @returns {*}
     */
    getDateInMap(map: AMap.Map) {
        return map.getAllOverlays().find((item:any) => {
            return item.getOptions().extData.id === this.featureData.id
        })
    }
    /**
     * 删除map中的元素
     * @param map
     */
    deleteDataInMap(map: AMap.Map) {
        map.remove(this.getDateInMap(map))
    }
    /**
     * 闪烁该要素
     * @param map
     */
    blinkData(map: AMap.Map) {
        var mapData = this.getDateInMap(map);
        //闪烁次数
        let blinkTimes = 6
        let currentTimes = 0
        //当前透明度
        const strokeOpacity = mapData.getOptions().strokeOpacity
        const interval = setInterval(() => {
            if (currentTimes < blinkTimes) {
                if (mapData.getOptions().strokeOpacity > 0.5) {
                    mapData.setOptions({
                        "strokeOpacity": 0
                    })
                } else {
                    mapData.setOptions({
                        "strokeOpacity": 1
                    })
                }
                currentTimes++
            } else {
                clearInterval(interval)
                mapData.setOptions({
                    "strokeOpacity": strokeOpacity
                })
            }
        }, 100)
    }
    /**
     * 下载json数据
     */
    getJSON() {
        toJson((this.featureData.name as string), this)
    }
    /**
     * 删除要素
     */
    deleteElement() {
        return reqDeleteMapData(this.featureData.id as string)
    }
    /**
     * 设置坐标点
     * @param path 坐标点
     * @returns 
     */
    setPath(path:any[]){
        this.featureMap.path=path
     }
}
export class Polyline implements FeatureInterface {
    id?: string | undefined;
    strokeColor?: string
    path?: any[]
    extData?: object
    constructor({ id, strokeColor, path, extData }: {
        id?: string | undefined;
        strokeColor?: string
        path?: any[]
        extData?: object
    }) {
        this.id = id
        this.strokeColor = strokeColor || getRandomColor()
        this.path = path
        this.extData = extData
    }
}